// Copyright (c) 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// ---
// Author: Jeffrey Junfeng Pan (panjunfeng@gmail.com)

// Header is used for storing the linked nodes
// in a frequent pattern tree, which have the same index.
// It is mainly used for helping build condtional pattern base
// and tree. Thus it is for frequent pattern itemset mining.

#ifndef FP_GROWTH_HEADER_H_
#define FP_GROWTH_HEADER_H_

#include <unordered_map>
#include <string>
#include <vector>

#include "common/scoped_ptr.h"
#include "node.h"

namespace std { using namespace __gnu_cxx; }
using namespace std;

namespace frequent_pattern_mining {

typedef unordered_map<int, Node*> HeaderTable;
typedef unordered_map<int, int> FrequencyTable;

class Header {
 public:

  // Constructor.
  Header();

  // Destructor.
  ~Header();

  // Get the frequency of a given item index
  int frequency(int index) const;

  // Set the frequency of an item index
  void set_frequency(int index, int frequency);

  // Increase the total frequency of an item with the given amount.
  void add_frequency(int index, int frequency);

  // Get the header node given an item index
  Node* header_node(int index) const;

  // Set the header node in the header table to the given value.
  // Note: this function will not link the nodes with the same
  // index together. Instead, it will replace the node in the header table.
  // If you want to add a node to an existing link list in the header table,
  // use add_node() function.
  void set_header_node(Node* node);

  // Add a Node to the header table.
  void add_node(Node* node);

  // Get the actual header table.
  const HeaderTable& header_table() const;

  // Get the actual frequency table.
  const FrequencyTable& frequency_table() const;
 private:

  // header table stores the linked list for each item and all its location in
  // a frequent pattern tree.
  scoped_ptr<HeaderTable> header_table_;

  // frequency table stores the total frequency an item appear in a frequent
  // pattern tree.
  scoped_ptr<FrequencyTable> frequency_table_;

  DISALLOW_EVIL_CONSTRUCTORS(Header);
};
}  // namepsace frequent_pattern_mining

#endif  // FP_GROWTH_HEADER_H_
